#include "ConfigLogic.h"

void  setTHDataToEeprom(THData data);
void config_set_state(u8 state);
void config_set_tempMin(u8 temp);
void config_set_tempMax(u8 temp);
void config_set_humiMin(u8 humi);
void config_set_humiMax(u8 humi);
void config_set_sunshine(u8 light);
void config_set_smoke(u8 smoke);

u8 config_get_state(void);
u8 config_get_tempMin(void);
u8 config_get_tempMax(void);
u8 config_get_humiMin(void);
u8 config_get_humiMax(void);
u8 config_get_sunshine(void);
u8 config_get_smoke(void);



int configTh(char* msg){
    //准备一个用来接收解析到的数据的结构体
    THData data;
    int res = parseTHData(&data,msg);

    if (!res) return 0;

    //把data数据设置给执行模块去使用
    setTHdata(data);

    //存储在本地存储芯片中
    setTHDataToEeprom(data);
    


    return 1;

}
//写入数据到芯片
void  setTHDataToEeprom(THData data){
    //调用存储的函数把数据写入AT24C02芯片
    config_set_state(data.state);
    config_set_tempMin(data.tempMin);
    config_set_tempMax(data.tempMax);
    config_set_humiMin(data.humiMin);
    config_set_humiMax(data.humiMax);
    config_set_sunshine(data.sunshine);
    config_set_smoke(data.smoke);

}
//从芯片中加载数据
void loadTHData(THData* data){
    data->state = config_get_state();
	data->tempMin = config_get_tempMin();
	data->tempMax = config_get_tempMax();
	data->humiMin = config_get_humiMin();
	data->humiMax = config_get_humiMax();
	data->sunshine = config_get_sunshine();
	data->smoke = config_get_smoke();
}


int  configNetwork(char* data){

    return 1;
}


int config_eepprom_init(){
    AT24CXX_Init();
    int times=0;
	while(AT24CXX_Check())  //检测AT24C02是否正常
	{
		times++;
        if (times>10)
        {
            return 0;
        } 
		delay_ms(20);
	}

    return 1;
}


void config_set_state(u8 state){
    AT24CXX_WriteOneByte(0,state);
}
void config_set_tempMin(u8 temp){
    AT24CXX_WriteOneByte(1,temp);
}
void config_set_tempMax(u8 temp){
    AT24CXX_WriteOneByte(2,temp);
}
void config_set_humiMin(u8 humi){
    AT24CXX_WriteOneByte(3,humi);
}
void config_set_humiMax(u8 humi){
    AT24CXX_WriteOneByte(4,humi);
}
void config_set_sunshine(u8 light){
    AT24CXX_WriteOneByte(5,light);
}
void config_set_smoke(u8 smoke){
    AT24CXX_WriteOneByte(6,smoke);
}

u8 config_get_state(void){
    return AT24CXX_ReadOneByte(0);
}
u8 config_get_tempMin(void){
    return AT24CXX_ReadOneByte(1);
}

u8 config_get_tempMax(void){
    return AT24CXX_ReadOneByte(2);
}
u8 config_get_humiMin(void){
    return AT24CXX_ReadOneByte(3);
}
u8 config_get_humiMax(void){
    return AT24CXX_ReadOneByte(4);
}
u8 config_get_sunshine(void){
    return AT24CXX_ReadOneByte(5);
}
u8 config_get_smoke(void){
    return AT24CXX_ReadOneByte(6);
}